﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using UmbracoMedia.Foundation;


namespace UmbracoMedia
{
    public partial class UmbracoMediaImageControl : UserControl
    {


        public event ImageEditedEventHandler ImageEdited;


        protected virtual void OnImageEdited(ImageEditedEventArgs e)
        {
            if (ImageEdited != null)
            {
                // Invokes the delegates. 
                ImageEdited(this, e);
            }
        }

        public UmbracoMediaItem MediaItem
        {
            get;
            set;
        }

        public UmbracoMediaImageControl(UmbracoMediaItem i)
        {
            InitializeComponent();
            this.MediaItem = i;
            this.DataContext = this.MediaItem;
        }

        private void Image_MouseEnter(object sender, MouseEventArgs e)
        {
            Image i = (Image)sender;
            i.Opacity = 0.5;
        

        }

        private void Image_MouseLeave(object sender, MouseEventArgs e)
        {
            Image i = (Image)sender;
            i.Opacity = 1;
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Image i = (Image)sender;
            i.Opacity = 1;

            UmbracoImageEditorControl a = new UmbracoImageEditorControl(this.MediaItem);
            

            a.ImageEdited += new ImageEditedEventHandler(WhenImageEdited);
            a.Show();

        }

        public void WhenImageEdited(object sender, ImageEditedEventArgs e)
        {
            // this.Thumb.Source = e.EditedImage.Source;
            
            this.Thumb.Source = e.EditedImage.Source;
            this.MediaItem.SourceImage = e.EditedImage.Source;
            
            ImageEditedEventArgs ev = new ImageEditedEventArgs(this.MediaItem);
            ev.EditedImage = e.EditedImage;
            OnImageEdited(ev);

        }

    }

    
}
